package com.kongwg4.all.junit;

import com.kongwg4.all.bean.Customer;
import com.kongwg4.all.dao.impl.CustomerDAOImpl;
import com.kongwg4.all.util.JDBCUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

/**
 * @author kongwg
 * @create 2021-01-10 12:11
 */
public class CustomerDAOImplTest {
    CustomerDAOImpl dao = new CustomerDAOImpl();

    @Test
    public void insert() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            Customer cust = new Customer(10, "赵油炸", "youzha@126.com", new Date(238947293847L));
            dao.insert(conn, cust);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }

    }

    @Test
    public void delete() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            dao.delete(conn, 22);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }

    @Test
    public void update() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            Customer cust = new Customer(32, "赵asdasd", "sadada@126.com", new Date(238947293847L));
            dao.update(conn,cust);
            System.out.println("修改成功");


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }

    @Test
    public void getInstance() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            Customer instance = dao.getInstance(conn, 23);
            System.out.println(instance);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }

    @Test
    public void getAll() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            List<Customer> list = dao.getAll(conn);
            list.forEach(System.out::println);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }

    @Test
    public void getCount() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();
            long count = dao.getCount(conn);
            System.out.println(count);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }

    @Test
    public void getBirth() {
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnection();

            Date maxBirth = dao.getBirth(conn);
            System.out.println(maxBirth);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, null);
        }
    }
}